<template>
	<view class="wrap">
		<view class="top">
			<view class="item">
				<view class="left">收货人</view>
				<input type="text" placeholder-class="line" placeholder="请填写收货人姓名" v-model="name" />
			</view>
			<view class="item">
				<view class="left">手机号码</view>
				<input type="text" placeholder-class="line" placeholder="请填写收货人手机号" v-model="mobile" />
			</view>
			<view class="item">
				<view class="left">所在地区</view>
				<pickRegions :defaultRegion="defaultRegionCode" @getRegion="handleGetRegion">
					<input disabled type="text" placeholder-class="line" placeholder="省市区县、乡镇等" v-model="region">
				</pickRegions>
				</input>
			</view>
			<view class="item address">
				<view class="left">详细地址</view>
				<textarea type="text" placeholder-class="line" placeholder="街道、楼牌等" v-model="address" />
			</view>
		</view>
		<view class="bottom">
			<view class="tag">
				<view class="left">标签</view>
				<view class="right">
					<block v-for="(value,index) in tags" :kdy="index">
					<text class="tags"  @click="settag(value.name)" :class="tag==index ? 'blue' : ''">{{value.name}}</text>
					</block>
				</view>
			</view>
			<view class="default">
				<view class="left">
					<view class="set">设置默认地址</view>
					<view class="tips">提醒：每次下单会默认推荐该地址</view>
				</view>
				<view class="">
					<switch color="red" @change="setDefault" />
				</view>
			</view>
		</view>
		<view class="bot_btn">
		<view @click="saveaddress" class="btn">保存地址</view>
		</view>
	</view>
</template>

<script>
	import {
		addressadd,addresseinfo,addressedit
	} from '@/api/mine.js'
	import pickRegions from '../../../pages/Mine/address/city/pick-regions.vue'

	export default {
		components: {
			pickRegions
		},
		onLoad(type) {
			console.log(type)
			this.type=type.type
			this.id=type.id
			console.log('type',this.type)
			if(this.type==1){
				uni.setNavigationBarTitle({
					title:'编辑地址'
				})
				this.getaddinfo()
			}
		},
		data() {
			return {
				region: '', //选择的地址
				name: '',
				mobile: '',
				address: '',
				is_default: '',
				tag: '',
				type:0,//0是添加地址，1是编辑更新地址
				id:'',//编辑地址id
				tags:[{
					name:'家'
				},{
					name:'公司'
				},{
					name:'学校'
				}]
			};
		},
		methods: {
			// 设置标签
			settag(value){
				console.log(value)
				if(value=='家'){
					this.tag=0
				}
				if(value=='公司'){
					this.tag=1
				}
				if(value=='学校'){
					this.tag=2
				}
			},
			// 获取编辑地址详情
			getaddinfo(){
				let that=this
				addresseinfo({
					id:that.id
				}).then(res=>{
					let list=res.data
					console.log(list)
					that.region=list.region
					that.name=list.name
					that.mobile=list.mobile
					that.address=list.address
					that.is_default=list.is_default
					that.tag=list.tag
					// console.log(that.region,that.name)
				})
			},
			// 获取选择的地区
			handleGetRegion(region) {
				this.region = region.map(item => item.name).join(' ')
			},
			// 设置默认地址
			setDefault() {
				this.is_default = 1
			},
			saveaddress() {
				if(this.type==1){
					addressedit({
						id:this.id,
						name: this.name,
						mobile: this.mobile,
						address: this.address,
						doorplate: this.doorplate,
						is_default: this.is_default,
						tag: this.tag,
						region:this.region,	
					}).then(res=>{
						console.log(res)
						uni.showToast({
							title:'保存成功',
							duration:2000,
						})
						uni.navigateTo({
							url:'/pages/Mine/address/index'
						})
					})
				}else{
					addressadd({
						name: this.name,
						mobile: this.mobile,
						address: this.address,
						doorplate: this.doorplate,
						is_default: this.is_default,
						tag: this.tag,
						region:this.region,					
					}).then(res => {
						console.log(res)
						uni.showToast({
							title:'保存成功',
							duration:2000,
						})
						uni.navigateTo({
							url:'/pages/Mine/address/index'
						})
					})
				}
				
			}
		}
	};
</script>

<style lang="scss" scoped>
	/deep/ .line {
		color: #c0c4cc;
		font-size: 28rpx;
	}

	.wrap {
		background-color: #f2f2f2;

		.top {
			background-color: #ffffff;
			border-top: solid 2rpx #e4e7ed;
			padding: 22rpx;

			.item {
				display: flex;
				font-size: 32rpx;
				line-height: 100rpx;
				align-items: center;
				border-bottom: solid 2rpx #e4e7ed;

				.left {
					width: 180rpx;
				}

				input {
					text-align: left;
				}
			}

			.address {
				padding: 20rpx 0;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}
			}

			.site-clipboard {
				padding-right: 40rpx;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}

				.clipboard {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: #c0c4cc;
					height: 80rpx;

					.icon {
						margin-top: 6rpx;
						margin-left: 10rpx;
					}
				}
			}
		}
		}

		.bottom {
			margin-top: 20rpx;
			padding: 40rpx;
			padding-right: 0;
			background-color: #ffffff;
			font-size: 28rpx;

			.tag {
				display: flex;

				.left {
					width: 160rpx;
				}

				.right {
					display: flex;
					flex-wrap: wrap;

					.tags {
						width: 140rpx;
						padding: 16rpx 8rpx;
						border: solid 2rpx #e4e7ed;
						text-align: center;
						border-radius: 50rpx;
						margin: 0 10rpx 20rpx;
						display: flex;
						font-size: 28rpx;
						align-items: center;
						justify-content: center;
						color: #606266;
						line-height: 1;
					}
					text:active{
						background-color: #007AFF;
					}
					.blue{
						background-color: #007AFF;
						color: #fff;
					}
				}
			}

			.default {
				margin-top: 50rpx;
				display: flex;
				justify-content: space-between;
				border-bottom: solid 2rpx #e4e7ed;
				line-height: 64rpx;

				.tips {
					font-size: 24rpx;
				}
			}
}
.bot_btn{
	position: absolute;
	display: flex;
	justify-content: center;
	align-items: center;
	width: 95%;
	bottom: 30rpx;
	z-index: 999;
	padding: 20rpx;
}
.btn{
	margin-top: 10rpx;
	width: 50%;
	height: 90rpx;
	background-color: #00C049;
	display: block;
	float: right;
	border-radius: 50rpx;
	align-items: center;
	color: #fff;
	text-align: center;
	line-height: 100rpx;
	font-size: 35rpx;
}			
</style>
